
/*
 * @项目名称: 模板开发
 * @FilePath: /meek/src/geometry/trapezium.js
 * @Author: 
 * @Date: 2020-08-24T16:51:56+08:00
 * @LastEditors: kongxueyang@datatang.com
 * @LastEditTime: 2021-08-11T11:09:23+08:00
 * Copyright (c) 2018-2022 
 */


import Geometry from './geometry'
import Polygon from './polygon'
import {getYIntersetOfLine} from '../geometry/support/geometryutil'
import { deepClone } from './support/cubeutil'

export default class Trapezium extends Polygon {
  constructor(coordinates) {
    super(coordinates)
  }

  get geometryType() {
    return Geometry.TRAPEZIUM
  }

  getNextPoint(coordinates) {
    if (coordinates.length === 2) {
      return this.getSecondPoint(coordinates);
    } else if (coordinates.length === 4) {
      return this.getLastPoint(coordinates);
    } else {
      return;
    }
  }

  getSecondPoint(coordinates) {
    const first = coordinates[0];
    const second = coordinates[1];
    return [first[0], second[1]];
  }

  getLastPoint(coordinates) {
    const third = coordinates[2];
    const last = coordinates[3];
    return [third[0], last[1]];
  }

  getFormShowPosition() {
    let rings = this.getCoordinates();
    let max = 0;
    let maxIndex = 0;
    rings[0].forEach((xy, index) => {
      if (xy[0] > max) {
        maxIndex = index;
        max = xy[0];
      }
    })
    return rings[0][maxIndex]
  }

   /**
   * 重新计算顶点
   *
   * @param p 对象
   * @param vertex 新移动的点
   * @param oldVertex 上次移动的点
   * @param dragSegment 数据对象
   * @param extent 绘制范围
   * @returns {*} 返回新的点集合（3维数组）
   */
  static updateCoordinatesForModification(p, vertex, oldVertex, dragSegment, extent) {
    // 只有y轴可以变动，x轴不能变动

    const oldCoordinates = deepClone(p.getCoordinates())

    const segment = dragSegment[0];
    const i = segment.index;
    const isVertex = segment.isVertex;
    let { index, ringIndex } = i;
    if (!isVertex) {
      index = i;
      ringIndex = segment.ringIndex;
    }

    if (isVertex) {
      oldCoordinates[ringIndex][index] = vertex;
      if (index === 0) {
        oldCoordinates[ringIndex][1][0] = vertex[0];
      } else if (index === 1) {
        oldCoordinates[ringIndex][0][0] = vertex[0];
      } else if (index === 2) {
        oldCoordinates[ringIndex][3][0] = vertex[0];
      } else {
        oldCoordinates[ringIndex][2][0] = vertex[0];
      }
    } else {
      if (index === 0) {
        oldCoordinates[ringIndex][0][0] = vertex[0];
        oldCoordinates[ringIndex][1][0] = vertex[0];
      } else if (index === 1) {
        const d = getYIntersetOfLine(
          oldCoordinates[ringIndex][1],
          oldCoordinates[ringIndex][2],
          vertex[0]
        )
        oldCoordinates[ringIndex][1][1] += vertex[1] - d
        oldCoordinates[ringIndex][2][1] += vertex[1] - d
        if (extent) {
          if (
            oldCoordinates[ringIndex][1][1] < 0 ||
            oldCoordinates[ringIndex][2][1] < 0 ||
            oldCoordinates[ringIndex][1][1] > extent[3] ||
            oldCoordinates[ringIndex][2][1] > extent[3]
          ) {
            return;
          }
        }
      } else if (index === 2) {
        oldCoordinates[ringIndex][2][0] = vertex[0];
        oldCoordinates[ringIndex][3][0] = vertex[0];
      } else if (index === 3) {
        const d = getYIntersetOfLine(
          oldCoordinates[ringIndex][0],
          oldCoordinates[ringIndex][3],
          vertex[0]
        )
        oldCoordinates[ringIndex][0][1] += vertex[1] - d
        oldCoordinates[ringIndex][3][1] += vertex[1] - d
        if (extent) {
          if (
            oldCoordinates[ringIndex][0][1] < 0 ||
            oldCoordinates[ringIndex][3][1] < 0 ||
            oldCoordinates[ringIndex][0][1] > extent[3] ||
            oldCoordinates[ringIndex][3][1] > extent[3]
          ) {
            return;
          }
        }
      }
    }
    if (
      (oldCoordinates[ringIndex][1][1] - oldCoordinates[ringIndex][0][1]) *
      (oldCoordinates[ringIndex][2][1] - oldCoordinates[ringIndex][3][1]) <= 0
    ) {
      return;
    }
    oldCoordinates[ringIndex][4] = oldCoordinates[ringIndex][0]
    p.setCoordinates(oldCoordinates);
  }

  static getRefLineCoords(coordinates, extent) {
    let x;
    if (coordinates.length === 3) {
      return false;
    } else if (coordinates.length > 3) {
      x = coordinates[2][0];
    } else {
      x = coordinates[0][0];
    }

    if (extent) {
      return [[x, extent[1]], [x, extent[3]]];
    } else {
      return [[x, -99999], [x, 99999]];
    }
  }

  clone() {
    const coords = this.getCoordinates()

    const trapezium = new Trapezium()
    trapezium.setCoordinates(coords)

    return trapezium
  }
}
